package ec_si_template;

import java.awt.Color;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.LogarithmicAxis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;

/**
 * A class for plotting the fitness function.
 *
 */
public class PlotFitness extends ApplicationFrame {

    /**
     * Create a new Frame and plot the fitness history provided in the array fitness
     *
     * @param fitness the fitness history (fitness per iteration/evaluation)
     */
    public PlotFitness(final double[] fitness) {

        super("title");

        final XYSeries s1 = new XYSeries("Fitness");
     

        for (int i = 1; i <= fitness.length; i++) {
            s1.add(i, fitness[i-1]);
          
        }

        final XYSeriesCollection dataset = new XYSeriesCollection();
        dataset.addSeries(s1);
       
        final JFreeChart chart = ChartFactory.createXYLineChart(
            "Fitness Histroy",          // chart title
            "Category",               // domain axis label
            "Value",                  // range axis label
            dataset,                  // data
            PlotOrientation.VERTICAL,
            true,                     // include legend
            true,
            false
        );

        final XYPlot plot = chart.getXYPlot();
        final NumberAxis domainAxis = new NumberAxis("Iteration");
        final NumberAxis rangeAxis = new LogarithmicAxis("Fitness (log scale)");
        plot.setDomainAxis(domainAxis);
        plot.setRangeAxis(rangeAxis);
        chart.setBackgroundPaint(Color.white);
        plot.setOutlinePaint(Color.black);
        final ChartPanel chartPanel = new ChartPanel(chart);
        chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
        setContentPane(chartPanel);

    }


}